import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';

import 'package:get/get.dart';

import 'app/routes/app_pages.dart';
import 'app_session.dart';
import 'app_translation.dart';

void main() async {
  await initServices();

  // 设置系统UI样式
  SystemChrome.setSystemUIOverlayStyle(
    const SystemUiOverlayStyle(
      statusBarColor: Colors.transparent,
      statusBarIconBrightness: Brightness.dark,
      statusBarBrightness: Brightness.light,
      systemNavigationBarColor: Colors.white,
      systemNavigationBarIconBrightness: Brightness.dark,
    ),
  );

  runApp(
    ScreenUtilInit(
      designSize: const Size(375, 812),
      minTextAdapt: true,
      splitScreenMode: true,
      child: GestureDetector(
        onTap: () {
          app.keyboardDismiss();
        },
        child: GetMaterialApp(
          debugShowCheckedModeBanner: true,
          defaultTransition: Transition.native,
          opaqueRoute: Get.isOpaqueRouteDefault,
          popGesture: Get.isPopGestureEnable,
          title: 'GLUZ',
          translations: AppTranslation(),
          // locale: app.isLangRU() ? const Locale('ru', 'RU') : const Locale('zh', 'CN'),
          locale: const Locale('zh', 'CN'),
          fallbackLocale: const Locale('zh', 'CN'),
          themeMode: ThemeMode.light,
          getPages: AppPages.routes,
          initialRoute: app.isLogin() ? Routes.MAIN_TAB : Routes.LOGIN,
          unknownRoute: AppPages.routes.first,
          useInheritedMediaQuery: kReleaseMode ? false : true,
          navigatorObservers: [FlutterSmartDialog.observer],
          builder: FlutterSmartDialog.init(),
          routingCallback: (routing) {
            print('路由:$routing');
            app.keyboardDismiss();
          },
          theme: ThemeData(
            // fontFamily: FontFamily.PingFang,
            splashColor: Colors.transparent,
            highlightColor: Colors.transparent,
            primaryColor: Colors.white,
            splashFactory: const NoSplashFactory(),
            scaffoldBackgroundColor: Colors.white,
            appBarTheme: AppBarTheme(
              systemOverlayStyle: SystemUiOverlayStyle(
                statusBarColor: Colors.transparent,
                statusBarIconBrightness: Brightness.dark,
                statusBarBrightness: Brightness.light,
              ),
              titleTextStyle: TextStyle(fontSize: 17, fontWeight: FontWeight.bold, color: Colors.black),
              backgroundColor: Colors.white,
              foregroundColor: Colors.black,
              elevation: 0, // 移除阴影
              centerTitle: true,
              toolbarHeight: 44,
              // titleTextStyle: AppTextStyle.normalWhite20,
            ),
            bottomNavigationBarTheme: BottomNavigationBarThemeData(
              backgroundColor: Colors.white,
              unselectedItemColor: Colors.black,
              selectedItemColor: Colors.blue,

            ),
            outlinedButtonTheme: OutlinedButtonThemeData(
              style: OutlinedButton.styleFrom(
                splashFactory: const NoSplashFactory(),
              ),
            ),
            inputDecorationTheme: InputDecorationTheme(
              contentPadding: EdgeInsets.symmetric(vertical: 15),
            ),
            filledButtonTheme: FilledButtonThemeData(
              style: FilledButton.styleFrom(
                backgroundColor: Colors.blue,
              )
            ),
          ),
        ),
      ),
    ),
  );
}

class NoSplashFactory extends InteractiveInkFeatureFactory {
  const NoSplashFactory();

  @override
  InteractiveInkFeature create({
    required MaterialInkController controller,
    required RenderBox referenceBox,
    required Offset position,
    required Color color,
    required TextDirection textDirection,
    bool containedInkWell = false,
    RectCallback? rectCallback,
    BorderRadius? borderRadius,
    ShapeBorder? customBorder,
    double? radius,
    VoidCallback? onRemoved,
  }) {
    return NoSplash(
      controller: controller,
      referenceBox: referenceBox,
      color: color,
      onRemoved: onRemoved,
    );
  }
}

/// 不显示水波纹效果
class NoSplash extends InteractiveInkFeature {
  NoSplash({
    required MaterialInkController controller,
    required super.referenceBox,
    required super.color,
    super.onRemoved,
  }) : super(controller: controller) {
    controller.addInkFeature(this);
  }

  @override
  void paintFeature(Canvas canvas, Matrix4 transform) {}
}
